/**
 * @author Linxiaosheng
 */
window.youboy = {};

window.youboy.login =function() {
		return {
			captchas : function(url, next, div) {
				this.url = url;
				this.next = next;
				this.div = div;
				
				return {
					nextCaptcha : $("#".concat(next)).click(function() {
						$("#captchas").html("<img src='" + url + "?m="+Math.random()+"'/>");
					})
				};
				return this;
			},
			checkShow : function(input,target) {
				return {
					show : $("#" + input).focus(function() {
						$("#" + target).css("display","inline-block");
					})
				};
				return this;
			},
			
			login : function(ctx) {
				return {
					keySubmit : function(){
						
						$("body").keydown(function(event){
							if(event.keyCode === 13){
								$("#sub").click();
							} 
						});
					}(),
					submit : $("#sub").click(function() {
						if(!$("#username").val() || !$("#password").val()) {
							$("#error").html("鐢ㄦ埛鍚嶅拰瀵嗙爜涓嶅厑璁镐负绌猴紒");
							refreshCaptcha();
							return false;
						}
						var $form = $("#login_form");
						$form.ajaxSubmit({
							url : ctx + "/login", 
							type : "POST",
							dataType : "JSON",
							success : function(message){
								if(message && !message.status){
									switchCode(message.code);
									return false;
								}
								
								location.href = ctx + "/admin/dashboard";
								return false;
							},
							error : function(code){
								//nothing
							}
						});
					})
				};
			}
		}
};

var refreshCaptcha = function() {
	$("#checking").attr("value","");
	$("#next").click();
};

var switchCode = function(code) {
	switch (code) {
		case 1:
			$("#error").html("璇ヨ处鎴蜂笉瀛樺湪!");
//			refreshCaptcha();
			break;
		case 2:
			$("#error").html("璇ヨ处鎴疯閿佸畾锛岃鑱旂郴瀹㈡埛浜哄憳!");
//			refreshCaptcha();
			break;
		case 4:
			$("#error").html("瀵嗙爜涓庡笎鍙蜂笉鍖归厤!");
			refreshCaptcha();
			break;
		case 8:
			$("#error").html("楠岃瘉鐮侀敊璇�");
			refreshCaptcha();
			break;
		default:
			$("#error").html("鏈煡閿欒!");
//			refreshCaptcha();
			break;
	}
};

